package com.yxy.service;

import com.github.pagehelper.PageHelper;
import com.yxy.base.BusinessException;
import com.yxy.dao.TableUserAddressMapper;
import com.yxy.dto.TableUserAddressDto;
import com.yxy.feign.VO.AdminUserInToken;
import com.yxy.model.TableUserAddress;
import com.yxy.utils.TokenCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class TableUserAddressService {

  @Autowired
  private TokenCheckUtil tokenCheckUtil;
  @Resource
  private TableUserAddressMapper tableUserAddressMapper;


  //添加方法（保存）
  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public TableUserAddress  save(TableUserAddressDto pojo) throws BusinessException {
    AdminUserInToken adminUser = tokenCheckUtil.getUserInfoByToken();
    TableUserAddress entity = new TableUserAddress();
    //BeanUtils.copyProperties("转换前的类", "转换后的类");
    BeanUtils.copyProperties(pojo, entity);
    entity.setCreateDate(new Date());
    entity.setCreateUserId(adminUser.getAdminUser().getId());
    entity.setCreateUserName(adminUser.getAdminUser().getUsername());
    tableUserAddressMapper.insertSelective(entity);
    return entity;
  }



  //添加方法（保存）
  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public TableUserAddress  appCreate(TableUserAddressDto pojo) throws BusinessException {

    TableUserAddress entity = new TableUserAddress();
    //BeanUtils.copyProperties("转换前的类", "转换后的类");
    BeanUtils.copyProperties(pojo, entity);
    entity.setCreateDate(new Date());
    entity.setCreateUserId(1l);
    entity.setCreateUserName("admin");
    try{
    tableUserAddressMapper.insertSelective(entity);
    }catch (Exception be){
      throw new  BusinessException(500,"请勿重复提交！");
    }
    return entity;
  }

  //获取列表
  public List<TableUserAddress> getlist(TableUserAddressDto query) throws BusinessException {
    AdminUserInToken adminUser = tokenCheckUtil.getUserInfoByToken();
    // 处理tenant id list
    Set<Long> tenantIdList = new HashSet<>();

    PageHelper.startPage(query.getPageNumber(), query.getPageSize());
    PageHelper.orderBy("t1.create_date desc");//排序
    List<TableUserAddress> dbResult = tableUserAddressMapper.selectList(query);
    return dbResult;
  }


  //编辑数据（更新）
  @Transactional(propagation= Propagation.REQUIRED,rollbackFor=Exception.class)
  public TableUserAddress modify(TableUserAddressDto pojo)throws BusinessException
  {
    TableUserAddress gpfMarketStorecode = tableUserAddressMapper.selectByPrimaryKey(pojo.getId());
    if(gpfMarketStorecode == null)
    {
      throw new BusinessException(501,"修改数据失败，未找到Id为["+pojo.getId()+"]的数据");
    }
    AdminUserInToken adminUser = tokenCheckUtil.getUserInfoByToken();
    TableUserAddress entity = new TableUserAddress();
    BeanUtils.copyProperties(pojo, entity);

    entity.setUpdateDate(new Date());
    entity.setUpdateUserId(adminUser.getAdminUser().getId());
    entity.setUpdateUserName(adminUser.getAdminUser().getUsername());
    tableUserAddressMapper.updateByPrimaryKeySelective(entity);
    return entity;
  }


  //编辑数据（更新）
  @Transactional(propagation= Propagation.REQUIRED,rollbackFor=Exception.class)
  public TableUserAddress appUpdate(TableUserAddressDto pojo)throws BusinessException
  {
    TableUserAddress gpfMarketStorecode = tableUserAddressMapper.selectByPrimaryKey(pojo.getId());
    if(gpfMarketStorecode == null)
    {
      throw new BusinessException(501,"修改数据失败，未找到Id为["+pojo.getId()+"]的数据");
    }

    TableUserAddress entity = new TableUserAddress();
    BeanUtils.copyProperties(pojo, entity);
    entity.setUpdateDate(new Date());
    entity.setUpdateUserId(1l);
    entity.setUpdateUserName("admin");
    tableUserAddressMapper.updateByPrimaryKeySelective(entity);
    return entity;
  }


  //根据id删除数据
  @Transactional(propagation= Propagation.REQUIRED,rollbackFor=Exception.class)
  public Integer delete(String Id)throws BusinessException {
    String arr[] = Id.split(",");
    List<Long> idlist = new ArrayList<>();
    for(int i = 0;i<arr.length;i++){
      Long p = Long.parseLong(arr[i]);
      idlist.add(p);
    }
    if(CollectionUtils.isEmpty(idlist)){
      throw new BusinessException(501, "删除失败，id为null");
    }
    int i = tableUserAddressMapper.deleteByIds(idlist);
    if (i<=0){
      throw new BusinessException(501, "删除数据失败");
    }
    return i;
  }

}
